#!/usr/bin/env bash

v=$1
if [ ! -n "$v" ]; then
    echo "usage:    bash $0 {php version}";
    echo "example:  bash $0 7.4";
    echo "注：需先添加php源：sudo add-apt-repository ppa:ondrej/php";
    exit 1;
fi

function version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
function version_le() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" == "$1"; }
function version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; }
function version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }

packages="bcmath bz2 cgi cli common curl dba dev enchant fpm gd gmp imap interbase intl ldap mbstring mysql odbc opcache pgsql phpdbg pspell readline snmp soap sqlite3 sybase tidy xml xsl zip";
str="php${v}";
if version_lt $v "8.0"; then
    packages+=" json"
fi
for package in ${packages} ; do
    str="${str} php${v}-${package}";
done

apt install ${str}
